let app = getApp();
import Notify from "/@vant/weapp/notify/notify";
import Dialog from '/@vant/weapp/dialog/dialog';
const previewUtil = require("./../../utils/preview.js");
import wxRequest from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allDynamic: [],
    dynamicId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (wx.getStorageSync('token') === null || wx.getStorageSync('token') === '') {
      Dialog.alert({
        title: '温馨提示',
        message: '未授权或未登录,请先授权登录',
        theme: 'round-button',
      }).then(() => {
        wx.switchTab({
          url: '../mine/mine'
        })
      });
    } else {
      // 已授权
      this.getMyDynamicList();
    }
  },
  /**
   * 预览单张图片
   * @param {*} event 
   */
  toPreviewImage(event) {
    previewUtil.previewImage(event);
  },
  /**
   * 预览多张图片
   * @param {*} event 
   */
  toPreviewMoreImage(event) {
    previewUtil.previewMoreImage(event);
  },
  /**
   * 将微信小程序的头像显示为高清
   * @param {*} event 
   */
  toPreviewWeChatAvatarUrlImage(event) {
    previewUtil.previewWeChatAvatarUrlImage(event);
  },
  /**
   * 获取所有用户已发布动态
   */
  getMyDynamicList() {
    wx.showLoading({
      title: "加载中"
    })
    let that = this;
    wx.request({
      url: app.globalData.baseUrl + '/dynamic/personalDynamic',
      method: "GET",
      header: {
        "token": wx.getStorageSync('token')
      },
      success: function (res) {
        that.setData({
          allDynamic: res.data.result
        })
        wx.hideLoading();
      }
    })
  },
  /**
   * 删除动态
   */
  deleteDynamic(e) {
    let that = this;
    that.setData({
      dynamicId: e.target.id
    })
    Dialog.confirm({
      title: '删除',
      message: '确认删除这条动态吗？'
    })
        .then(() => {
          wx.request({
            url: app.globalData.baseUrl + '/dynamic/deleteDynamic',
            method: "POST",
            data: {
              dynamicId: that.data.dynamicId
            },
            header: {
              token: wx.getStorageSync("token"),
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              if (res.data.code === 2007) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                });
              } else {
                wx.showToast({
                  title: '删除失败,请联系客服',
                  icon: 'none'
                });
              }
              Dialog.close();
              that.getMyDynamicList();
              
            }
          })
        })
        .catch(() => {
          Dialog.close();
        });
      
  }
})